package com.health.trans.job;

import com.health.trans.kit.ReaderConfig;
import com.health.trans.kit.WriterConfig;
import com.health.trans.processor.reader.CsReader;
import com.health.trans.processor.writer.CsWriter;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author nqlz
 */
@Component
public class ItemCs {

    private static Map<String, CsReader> readerMap;
    private static Map<String, CsWriter> writerMap;

    public ItemCs(List<CsReader> readers, List<CsWriter> writers) {
        readerMap = readers.stream().collect(Collectors.toMap(i -> i.getClass().getSimpleName(), i -> i));
        writerMap = writers.stream().collect(Collectors.toMap(i -> i.getClass().getSimpleName(), i -> i));
    }


    public static <T> ItemWriter<T> getWriter(String key, WriterConfig<T> writerConfig) {
        Optional<String> first = writerMap.keySet().stream().filter(i -> i.equalsIgnoreCase(key)).findFirst();
        CsWriter tCsWriter = writerMap.get(first.get());
        return tCsWriter.getWriter(writerConfig);
    }

    public static <T> ItemReader<T> getReader(String key, ReaderConfig<T> readerConfig) {
        Optional<String> first = readerMap.keySet().stream().filter(i -> i.equalsIgnoreCase(key)).findFirst();
        CsReader csReader = readerMap.get(first.get());
        return csReader.getReader(readerConfig);
    }
}
